/*
 * LocalConnectionHandler.cpp
 *
 *  Created on: Apr 10, 2011
 *      Author: sms103
 */

#include "LocalConnectionHandler.h"

LocalConnectionHandler::LocalConnectionHandler(SFTPClient *client, TCPSocket *sock) {
	sftpClient = client;
	socket = sock;

}

void LocalConnectionHandler::handleConnection(){
	//yes random hardcoded numbers are bad
	//10kb should be way more than enough for a three letter command
	//and two path names, but is small enough to be insignificant
	void *recvBuf = malloc(RECV_BUF_SIZE);
	int bytesRcvd;
	string *str;
	istringstream *iss;
	string strbuf;
	string strbuf2;
	string localName;
	string remoteName;

	while((bytesRcvd = socket->tcpRecv(recvBuf,RECV_BUF_SIZE)) > 0){
		//cout<<"rcvd msg from php rcvlen "<<bytesRcvd<<endl;
		str =  new string((char *)recvBuf,bytesRcvd);
		iss = new istringstream(*str,istringstream::in);
		getline(*iss,strbuf,':');
		cout<<"rcvd msg from php: "<<strbuf <<" length "<<bytesRcvd<<endl;
		if(strbuf=="CON"){
			getline(*iss,strbuf,':');//hostname

			cout << "connecting to server: "<<strbuf<<endl;
			if(sftpClient->connectToServer(strbuf)){
				strbuf = "DONE";
			}
			else strbuf = "FAIL";
			socket->tcpSend((void *)strbuf.c_str(),strbuf.size());
		}
		else if(strbuf=="DIS"){
			sftpClient->disconnect();
			strbuf = "DONE";
			socket->tcpSend((void *)strbuf.c_str(),strbuf.size());
			break;

		}
		else if(strbuf=="DCO"){
			getline(*iss,strbuf,':');
			getline(*iss,strbuf2,':');
			cout << "connecting to server: "<<strbuf<<endl;
			if(sftpClient->connectToServer(strbuf,atoi(strbuf2.c_str()))){
				strbuf = "DONE";
			}
			else strbuf = "FAIL";
			socket->tcpSend((void *)strbuf.c_str(),strbuf.size());

		}

		else if(strbuf == "GET"){
			getline(*iss,remoteName,':');
			getline(*iss,localName,':');
			cout << "getting file, remote name: "<<remoteName << " local : "<<localName<<endl;
			if(sftpClient->getFile(localName,remoteName)){
				strbuf = "DONE";
			}
			else{
				strbuf = "FAIL";
			}
			socket->tcpSend((void *)strbuf.c_str(),strbuf.size());
		}
		else if(strbuf == "PUT"){
			getline(*iss,remoteName,':');
			getline(*iss,localName,':');
			cout << "putting file, remote name: "<<remoteName << " local : "<<localName<<endl;
			if(sftpClient->putFile(localName,remoteName)){
				strbuf = "DONE";
			}
			else{
				strbuf = "FAIL";
			}
			socket->tcpSend((void *)strbuf.c_str(),strbuf.size());

		}
		else if (strbuf == "LIS"){
			getline(*iss,remoteName,':');
			cout<<"listing contents of remote directory: "<<remoteName<<endl;
			strbuf.clear();
			uint16_t len;
			if(!sftpClient->listRemoteDir(remoteName,strbuf)){
				strbuf = "FAIL:";
			}
				//if list remote dir is successful
				len = strbuf.size();
				//put length of return string into two bytes
				unsigned char highOrderByte = len/256;
				unsigned char lowOrderByte = len & 255;
				((char *)recvBuf)[0]=highOrderByte;
				((char *)recvBuf)[1]=lowOrderByte;
				//send the length
				socket->tcpSend(recvBuf,2);
				uint16_t bytesSent = 0;
				//send the directory contents
				while(bytesSent < len){
					bytesSent += socket->tcpSend((void *)(strbuf.c_str() + bytesSent),len);
				}

		}
		else if(strbuf == "EXI"){
			sftpClient->disconnect();
			strbuf = "DONE";
			socket->tcpSend((void *)strbuf.c_str(),strbuf.size());
			sftpClient->listener->close();
			break;
		}

		delete(str);
		delete(iss);
		free(recvBuf);
	}

}

void LocalConnectionHandler::sendBadMessageReply(){
	string reply = "BAD:unrecognized message format.";
	socket->tcpSend((void *)reply.c_str(),reply.size());
}

LocalConnectionHandler::~LocalConnectionHandler() {
	// TODO Auto-generated destructor stub
}
